
//  Created by 邓盛明 on 2022/1/25.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit


class TF_PublicityView: UIView,UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "pubiicCell", for: indexPath) as! TF_PublicityCell
        cell.selectionStyle = .none
      
        cell.checkBtn.tag = indexPath.row
        cell.checkBtn.addTarget(self, action: #selector(checkBtnClick(btn:)), for: .touchUpInside)
        let model = self.dataSource[indexPath.row]
        cell.setPopModel(model)
        return cell
    }
    
    var view:UIView!
    @objc public var titleLab:UILabel!  //标题
    var lineView:UIView!
    var tableview:UITableView!
    @objc public var isShow:Bool = false
    @objc public var dataSource:[TFPopupPut] = []
    @objc public weak var delegate : TF_PublicityViewDelegate?

    var noLab:UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.view = UIView.init()
        self.view.backgroundColor = UIColor.white
        addSubview(self.view)
        
        self.titleLab = UILabel.init()
        self.titleLab.text = "宣传图列表"
        self.titleLab.font = UIFont.PingFang(size: 16)
        self.titleLab.textColor = UIColor(red: 0.14, green: 0.22, blue: 0.25,alpha:1)
        self.titleLab.textAlignment = .center
        self.view.addSubview(self.titleLab)
        
        self.lineView = UIView.init()
        self.lineView.backgroundColor = UIColor(red: 0.92, green: 0.94, blue: 0.95, alpha: 1)
        self.view.addSubview(self.lineView)
        
        self.tableview = UITableView.init()
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.separatorStyle = .none
        self.tableview.backgroundColor = UIColor.white
        self.tableview.register(TF_PublicityCell.self, forCellReuseIdentifier: "pubiicCell")
        self.tableview.rowHeight = 117
        self.view.addSubview(self.tableview)
        

    }
    
    @objc func checkBtnClick(btn:UIButton) {
    
        let t = btn.tag
        let p = self.dataSource[t]
        
     
       if self.delegate != nil && (self.delegate?.responds(to: #selector(TF_PublicityViewDelegate.checkPuplicityImg(_:))))!{
           self.delegate?.checkPuplicityImg(p)
       }
    }
    
    @objc public func setUI() {
        self.view.frame = CGRect(x: 0, y: self.frame.height, width: self.frame.width, height: 406+44)
        let maskLayer = self.getMaskLayer(rect: self.view.bounds)
        self.view.layer.mask = maskLayer;
        self.titleLab.frame = CGRect(x: 0, y: 11, width: self.view.frame.width, height: 22)
        self.lineView.frame = CGRect(x: 0, y: self.titleLab.frame.maxY+11, width: self.view.frame.width, height: 1)
        self.tableview.frame = CGRect(x: 0, y: self.lineView.frame.maxY, width: self.view.frame.width, height: self.view.frame.height-self.lineView.frame.maxY)

    }
    func getMaskLayer(rect:CGRect) -> CAShapeLayer {
        let size = CGSize(width: 10, height: 10)
      
        let maskPath = UIBezierPath.init(roundedRect: rect, byRoundingCorners: [.topRight,.topLeft], cornerRadii: size)
        let maskLayer = CAShapeLayer.init()
        maskLayer.frame = rect
        maskLayer.path = maskPath.cgPath
        return maskLayer
    }
    @objc public func updateData(_ arr:NSArray)  {
        self.dataSource.removeAll()
        for model in arr {
            self.dataSource.append(model as! TFPopupPut)
        }
        
        self.tableview.reloadData()

    }
    
   @objc public func showView(_ show:Bool) {
       self.isShow = show
        if show {
            UIView.animate(withDuration: 0.5) {
                self.view.frame = CGRect(x: 0, y: self.frame.height-44-406, width: self.frame.width, height: 406+44)
             

            };
        } else {
            UIView.animate(withDuration: 0.5) {
                self.view.frame = CGRect(x: 0, y: self.frame.height, width: self.frame.width, height: 406+44)
            };
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        // 1.判断下自己能否接收触摸事件
        if (self.isUserInteractionEnabled == false || self.isHidden == true || self.alpha <= 0.01){
            return nil
        }
    
        // 2.判断下点在不在当前控件上
        // 用pointInside方法判断点在不在控件上,point这个必须是方法调用者坐标系上的点
        if self.point(inside: point, with: event) == false {
            return nil
        }
 
        // 3.从后往前遍历自己的子控件
        let count = self.subviews.count
        for i in 0..<count {
            // 取出子控件
            let childV:UIView? = self.subviews[i]
            // 把自己坐标系上点转化成子控件坐标系上点
           let childP = self.convert(point, to: childV)
            // 如果找到最合适view就直接返回
            let fitView:UIView? = childV?.hitTest(childP, with: event)
            if (fitView != nil) {
                return fitView
            }
            if self.isShow {
                self.showView(false)
            }
        }

       
        
        // 4.自己就是最合适view
        return nil;
        
    }
}


@objc protocol TF_PublicityViewDelegate : NSObjectProtocol {
    
    func checkPuplicityImg(_ model:TFPopupPut)
}
